<?php

namespace App\Http\Controllers\Admin;

use App\Model\SourceModel;
use App\Model\ShopGoodModel;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Http\Controllers\Controller;

//素材管理
class SourceController extends Controller
{
	//图文列表
	public function index_pic(Request $request){
		// $list = SourceModel::where(function ($query) {
		// 	if (request()->keywords) {
		// 		$query->where('gid', request()->keywords);
		// 	}
		// })
        // ->where('type',1)
		// ->orderBy('id','desc')->paginate(15);

		$keywords = request()->keywords;
		$is_exit = request()->is_exit;
		$list = DB::table('shop_source')
		->join('shop_good','shop_source.gid','shop_good.id')
		->when($keywords, function ($query) use ($keywords){
			$query->where('shop_good.gid', request()->keywords);
		})
		->when($is_exit, function ($query) use ($is_exit){
			if($is_exit == 1){
				$query->where('shop_good.state',3);
			}else{
				$query->where('shop_good.state','<>',3);
			}
		})
		->where('shop_source.type',1)
		->orderBy('shop_source.id','desc')
		->select('shop_source.*')
		->paginate(15);

        foreach($list as $k=>$v){
            $list[$k]->gname = ShopGoodModel::where('id',$v->gid)->value('name');
        }
		$pageParam['keywords'] = request()->keywords;
		$pageParam['is_exit'] = request()->is_exit;
		//分页
		$page = $list->appends($pageParam)->links();
		return  view('admin.source.index_pic',compact('list','request','page'));
	}

    //设置排序
    public function update_sort(){
        $id = request()->id;
        $sort = request()->sort;
        $source = SourceModel::where('id',$id)->first();
        if(!$source){
            return ['code'=>0,'msg'=>'该素材信息有误'];
        }
        DB::beginTransaction();
        try{
			SourceModel::where('id',$id)->update(['sort'=>$sort]);
        	DB::commit();
		}catch(Exception $e){
        	DB::rollBack();
        	return ['code'=>0,'msg'=>'设置失败'];
		}
		return ['code'=>1,'msg'=>'设置成功'];
    }

    //编辑图文素材
	public function pic_edit(Request $request){
		$id = request()->id;
		$list = SourceModel::where('id',$id)->first();
		return view('admin.source.pic_edit',compact('id','list','request'));
	}

	//编辑图文素材
	public function pic_update(){
		$id = request()->id;
		$content = request()->contents;
		$list = SourceModel::where('id',$id)->first();
		if(!$list){
			return ['code'=>0,'msg'=>'该素材信息有误'];
		}
		DB::beginTransaction();
		try{
			SourceModel::where('id',$id)->update(['content'=>$content]);
			DB::commit();
		}catch(Exception $e){
			DB::rollBack();
			return ['code'=>0,'msg'=>'编辑失败'];
		}
		return ['code'=>1,'msg'=>'编辑成功'];
	}

	//视频列表
	public function index_vedio(Request $request){

		$keywords = request()->keywords;
		$is_exit = request()->is_exit;
		$list = DB::table('shop_source')
		->join('shop_good','shop_source.gid','shop_good.id')
		->when($keywords, function ($query) use ($keywords){
			$query->where('shop_good.gid', request()->keywords);
		})
		->when($is_exit, function ($query) use ($is_exit){
			if($is_exit == 1){
				$query->where('shop_good.state',3);
			}else{
				$query->where('shop_good.state','<>',3);
			}
		})
		->where('shop_source.type',2)
		->orderBy('shop_source.id','desc')
		->select('shop_source.*')
		->paginate(15);
		foreach($list as $k=>$v){
			$list[$k]->gname = ShopGoodModel::where('id',$v->gid)->value('name');
			$list[$k]->vedio = 'http://'.$_SERVER['HTTP_HOST'].$v->vedio;
		}
		$pageParam['keywords'] = request()->keywords;
		$pageParam['is_exit'] = request()->is_exit;
		//分页
		$page = $list->appends($pageParam)->links();
		return  view('admin.source.index_vedio',compact('list','request','page'));
	}

	//编辑标题
	public function vedio_edit_title(){
		$id = request()->id;
		$title = request()->title;

		$list = SourceModel::where('id',$id)->first();
		if(!$list){
			return ['code'=>0,'msg'=>'该视频信息有误'];
		}
		DB::beginTransaction();
		try{
			SourceModel::where('id',$id)->update(['title'=>$title]);
			DB::commit();
		}catch(Exception $e){
			DB::rollBack();
			return ['code'=>0,'msg'=>'编辑失败'];
		}
		return ['code'=>1,'msg'=>'编辑成功'];
	}

	//编辑封面图
	public function vedio_edit_pic(){
		$id = request()->id;
		$list = SourceModel::where('id',$id)->first();
		if($list->picname){
			$list->oldname = $list->picname;
			$list->picname = "http://".$_SERVER['HTTP_HOST'].$list->picname;
		}else{
			$list->olcname = "";
			$list->picname = "http://".$_SERVER['HTTP_HOST']."/img/good.png";
		}
		return view('admin.source.edit_mpic',compact('id','list'));
	}

	//编辑商品主图
	public function vedio_update_pic(){
		$id = request()->id;
		$source = SourceModel::where('id',$id)->first();
		DB::beginTransaction();
		try{
			SourceModel::where('id',$id)->update([
				'picname' => request()->picname,
			]);
			DB::commit();
		}catch(Exception $e){
			DB::rollBack();
		}
		return ['code'=>1,'msg'=>'上传成功'];
	}
}